﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using Assist;
using YArchitech.Controls;
using YArchitech.Electric;
using YArchitech.Electric.Controls;
using YArchitech.LIB;
using YArchitech.MEP.Electric.Common;
using YArchitech.Revit.Electric.Common;

namespace HYRElectric.Equipment
{
	public partial class EquipmentLayoutForm : System.Windows.Forms.Form
	{
		[DllImport("user32.dll", CharSet = CharSet.Auto)]
		public static extern IntPtr SendMessage(IntPtr hWnd, int msg, int wParam, int lParam);

		public EquipmentLayoutWay LayoutWay
		{
			get
			{
				return this.layoutWay;
			}
		}

		public DbFamilyObject FamilyObject
		{
			get
			{
				return this.familyObject;
			}
		}

		public EquipmentLayoutForm(string title, string tableName, List<DbFamilyGroup> groups, int layWays)
		{
			this.InitializeComponent();
			this.formTitle = title;
			this.key = tableName;
			this.familyGroups = groups;
			this.layoutWays = layWays;
			this.layoutWay = EquipmentLayoutWay.Lay_Random;
			this.familyObject = null;
		}

		private void EquipmentLayoutForm_Load(object sender, EventArgs e)
		{
			//base.Icon = Resource.YArchitechIcon;
			this.Text = this.formTitle;
			this.InitToolbar();
			this.InitListView();
			this.ReadSettingDataFromXml();
		}

		private void EquipmentLayoutForm_Shown(object sender, EventArgs e)
		{
			this.SetPropertyGridColumnWidth(this.propertyGrid, 100);
			this.propertyGrid.Invalidate();
		}

		private void InitToolbar()
		{
			if ((this.layoutWays & 1) != 0)
			{
				this.toolBar.Items.Add(this.NewToolStripItem(YArchitech.Electric.Resources.GetImage("Random24"), "Lay_Random", "任意布置", EquipmentLayoutWay.Lay_Random));
			}
			if ((this.layoutWays & 2) != 0)
			{
				this.toolBar.Items.Add(this.NewToolStripItem(YArchitech.Electric.Resources.GetImage("DragLine24"), "Lay_DragLine", "拉线布置", EquipmentLayoutWay.Lay_DragLine));
			}
			if ((this.layoutWays & 4) != 0)
			{
				this.toolBar.Items.Add(this.NewToolStripItem(YArchitech.Electric.Resources.GetImage("DragLineEx24"), "Lay_DragLineEx", "拉线均布", EquipmentLayoutWay.Lay_DragLineEx));
			}
			if ((this.layoutWays & 8) != 0)
			{
				this.toolBar.Items.Add(this.NewToolStripItem(YArchitech.Electric.Resources.GetImage("Rect24"), "Lay_Rect", "矩形布置", EquipmentLayoutWay.Lay_Rect));
			}
			if ((this.layoutWays & 16) != 0)
			{
				this.toolBar.Items.Add(this.NewToolStripItem(YArchitech.Electric.Resources.GetImage("RectEx24"), "Lay_RectEx", "矩形均布", EquipmentLayoutWay.Lay_RectEx));
			}
			if ((this.layoutWays & 32) != 0)
			{
				this.toolBar.Items.Add(this.NewToolStripItem(YArchitech.Electric.Resources.GetImage("ArcLine24"), "Lay_ArcLine", "弧线均布", EquipmentLayoutWay.Lay_ArcLine));
			}
			if ((this.layoutWays & 64) != 0)
			{
				this.toolBar.Items.Add(this.NewToolStripItem(YArchitech.Electric.Resources.GetImage("Fan24"), "Lay_Fan", "扇形布置", EquipmentLayoutWay.Lay_Fan));
			}
			if ((this.layoutWays & 512) != 0)
			{
				this.toolBar.Items.Add(this.NewToolStripItem(YArchitech.Electric.Resources.GetImage(""), "", "配电柜布置", EquipmentLayoutWay.Lay_SwitchBoxes));
			}
			if (this.toolBar.Items.Count == 1)
			{
				this.toolBar.Visible = false;
				ToolStripButton toolStripButton = this.toolBar.Items[0] as ToolStripButton;
				this.layoutWay = (EquipmentLayoutWay)toolStripButton.Tag;
			}
		}

		private ToolStripButton NewToolStripItem(Image image, string name, string tip, EquipmentLayoutWay layWay)
		{
			return new ToolStripButton
			{
				AutoSize = false,
				DisplayStyle = ToolStripItemDisplayStyle.Image,
				Image = image,
				Name = name,
				Text = tip,
				Tag = layWay
			};
		}

		private void InitListView()
		{
			this.lsvLegend.LargeImageList.ImageSize = new Size(75, 75);
			this.lsvLegend.Items.Clear();
			this.lsvLegend.BeginUpdate();
			foreach (DbFamilyGroup dbFamilyGroup in this.familyGroups)
			{
				if (!string.IsNullOrEmpty(dbFamilyGroup.Image2D) && dbFamilyGroup.FamilyObjects.Count > 0 && File.Exists(dbFamilyGroup.Image2D))
				{
					string name = dbFamilyGroup.FamilyObjects[0].Name;
					string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(dbFamilyGroup.Image2D);
					Image image = Image.FromFile(dbFamilyGroup.Image2D);
					this.lsvLegend.LargeImageList.Images.Add(fileNameWithoutExtension, image);
					ListViewItem listViewItem = new ListViewItem();
					listViewItem.Text = name;
					listViewItem.Tag = dbFamilyGroup;
					listViewItem.ImageKey = fileNameWithoutExtension;
					this.lsvLegend.Items.Add(listViewItem);
				}
			}
			this.lsvLegend.EndUpdate();
			if (this.lsvLegend.Items.Count <= 0)
			{
				base.DialogResult = DialogResult.Cancel;
				base.Close();
				return;
			}
			EquipmentLayoutForm.SendMessage(this.lsvLegend.Handle, this.LVM_SETICONSPACING, 0, 6881363);
		}

		private void UpdateListbox()
		{
			if (this.lsvLegend.SelectedItems.Count <= 0)
			{
				return;
			}
			DbFamilyGroup dbFamilyGroup = this.lsvLegend.SelectedItems[0].Tag as DbFamilyGroup;
			if (dbFamilyGroup == null)
			{
				return;
			}
			this.lsbFamily.Items.Clear();
			foreach (DbFamilyObject item in dbFamilyGroup.FamilyObjects)
			{
				this.lsbFamily.Items.Add(item);
			}
			this.SelectFamily(0);
		}

		private void UpdatePictureBox()
		{
			this.picture.Image = null;
			DbFamilyObject dbFamilyObject = this.lsbFamily.SelectedItem as DbFamilyObject;
			if (dbFamilyObject == null)
			{
				return;
			}
			if (!File.Exists(dbFamilyObject.Image))
			{
				this.picture.Image = this.picture.InitialImage;
				return;
			}
			this.picture.Image = Image.FromFile(dbFamilyObject.Image);
		}

		private void UpdatePropertyGrid(bool flag)
		{
			DbFamilyObject dbFamilyObject = this.lsbFamily.SelectedItem as DbFamilyObject;
			if (dbFamilyObject == null)
			{
				this.propertyGrid.SelectedObject = null;
				return;
			}
			if (flag)
			{
				YArchitech.Electric.Controls.HYPropertys hypropertys = this.propertyGrid.SelectedObject as YArchitech.Electric.Controls.HYPropertys;
				if (hypropertys == null)
				{
					return;
				}
				using (List<YArchitech.Electric.Controls.HYProperty>.Enumerator enumerator = hypropertys.GetEnumerator())
				{
					while (enumerator.MoveNext())
					{
						YArchitech.Electric.Controls.HYProperty hyproperty = enumerator.Current;
						(hyproperty.Tag as DbFamilyParameter).Value = hyproperty.Value;
					}
					return;
				}
			}
			YArchitech.Electric.Controls.HYPropertys hypropertys2 = new YArchitech.Electric.Controls.HYPropertys();
			foreach (DbFamilyParameter dbFamilyParameter in dbFamilyObject.Parameters)
			{
				if (!dbFamilyParameter.ReadOnly)
				{
					hypropertys2.Add(new YArchitech.Electric.Controls.HYProperty
					{
						Category = "设备参数",
						Name = dbFamilyParameter.Name,
						DisplayName = (string.IsNullOrEmpty(dbFamilyParameter.Unit) ? dbFamilyParameter.SharedName : (dbFamilyParameter.SharedName + "(" + dbFamilyParameter.Unit + ")")),
						Value = (string.IsNullOrEmpty(dbFamilyParameter.Value.ToString().Trim()) ? dbFamilyParameter.Value : Convert.ChangeType(dbFamilyParameter.Value, dbFamilyParameter.Tag as Type)),
						ReadOnly = dbFamilyParameter.ReadOnly,
						Tag = dbFamilyParameter
					});
				}
			}
			this.propertyGrid.SelectedObject = hypropertys2;
			this.propertyGrid.ExpandAllGridItems();
			this.SetPropertyGridColumnWidth(this.propertyGrid, 100);
		}

		private void SetPropertyGridColumnWidth(PropertyGrid grid, int width)
		{
			if (grid == null)
			{
				return;
			}
			Control control = (Control)grid.GetType().GetField("gridView", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(grid);
			if (control == null)
			{
				return;
			}
			FieldInfo field = control.GetType().GetField("labelWidth", BindingFlags.Instance | BindingFlags.NonPublic);
			if (null == field)
			{
				return;
			}
			field.SetValue(control, width);
			control.Invalidate();
		}

		private void ReadSettingDataFromXml()
		{
			YArchitech.MEP.Electric.Common.XmlHelper xmlHelper = new YArchitech.MEP.Electric.Common.XmlHelper(Path.Combine(Product.UserDataLocation, "ElectricEquipment.xml"), "ElectricData");
			string path = "EquipmentLayout\\" + this.key;
			xmlHelper.SelectNode(path);
			int index = Convert.ToInt32(xmlHelper.GetAttributeEx("LayoutWay", "0"));
			this.SelectLayoutWay(index);
			index = Convert.ToInt32(xmlHelper.GetAttributeEx("Legend", "0"));
			this.SelectLegend(index);
			index = Convert.ToInt32(xmlHelper.GetAttributeEx("Family", "0"));
			this.SelectFamily(index);
		}

		private void SaveSettingDataToXml()
		{
			YArchitech.MEP.Electric.Common.XmlHelper xmlHelper = new YArchitech.MEP.Electric.Common.XmlHelper(Path.Combine(Product.UserDataLocation, "ElectricEquipment.xml"), "ElectricData");
			string path = "EquipmentLayout\\" + this.key;
			xmlHelper.SelectNode(path);
			xmlHelper.SetAttribute("LayoutWay", this.GetLayoutWay().ToString());
			int num = 0;
			if (this.lsvLegend.SelectedItems.Count > 0)
			{
				num = this.lsvLegend.SelectedItems[0].Index;
			}
			xmlHelper.SetAttribute("Legend", num.ToString());
			xmlHelper.SetAttribute("Family", this.lsbFamily.SelectedIndex.ToString());
			xmlHelper.SaveXml("");
		}

		private void SelectLayoutWay(int index)
		{
			if (this.toolBar.Items.Count <= 0)
			{
				return;
			}
			if (index < 0 || index >= this.toolBar.Items.Count)
			{
				index = 0;
			}
			(this.toolBar.Items[index] as ToolStripButton).CheckState = CheckState.Checked;
		}

		private int GetLayoutWay()
		{
			for (int i = 0; i < this.toolBar.Items.Count; i++)
			{
				if ((this.toolBar.Items[i] as ToolStripButton).CheckState == CheckState.Checked)
				{
					return i;
				}
			}
			return 0;
		}

		private void SelectLegend(int index)
		{
			if (this.lsvLegend.Items.Count <= 0)
			{
				return;
			}
			if (index < 0 || index >= this.lsvLegend.Items.Count)
			{
				index = 0;
			}
			this.lsvLegend.Items[index].Selected = true;
			this.lsvLegend.EnsureVisible(index);
		}

		private void SelectFamily(int index)
		{
			if (this.lsbFamily.Items.Count <= 0)
			{
				return;
			}
			if (index < 0 || index >= this.lsbFamily.Items.Count)
			{
				index = 0;
			}
			this.lsbFamily.SelectedIndex = index;
		}

		private void SetPropertyValue(string name, string value)
		{
			YArchitech.Controls.HYPropertys hypropertys = (YArchitech.Controls.HYPropertys)this.propertyGrid.SelectedObject;
			if (hypropertys == null)
			{
				return;
			}
			if (!hypropertys.Exists((YArchitech.Controls.HYProperty item) => item.Description == name))
			{
				return;
			}
			hypropertys.Find((YArchitech.Controls.HYProperty item) => item.Description == name).Value = value;
		}

		private void lsvLegend_SelectedIndexChanged(object sender, EventArgs e)
		{
			this.UpdateListbox();
		}

		private void lsbFamily_SelectedIndexChanged(object sender, EventArgs e)
		{
			this.UpdatePictureBox();
			this.UpdatePropertyGrid(false);
		}

		private void toolBar_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
		{
			ToolStripButton toolStripButton = e.ClickedItem as ToolStripButton;
			if (toolStripButton == null)
			{
				return;
			}
			foreach (object obj in this.toolBar.Items)
			{
				(((ToolStripItem)obj) as ToolStripButton).Checked = false;
			}
			toolStripButton.Checked = true;
		}

		private void buttonOk_Click(object sender, EventArgs e)
		{
			if (this.toolBar.Visible)
			{
				foreach (object obj in this.toolBar.Items)
				{
					ToolStripButton toolStripButton = ((ToolStripItem)obj) as ToolStripButton;
					if (toolStripButton.Checked)
					{
						this.layoutWay = (EquipmentLayoutWay)toolStripButton.Tag;
						break;
					}
				}
			}
			this.UpdatePropertyGrid(true);
			this.familyObject = (this.lsbFamily.SelectedItem as DbFamilyObject);
			this.SaveSettingDataToXml();
			base.DialogResult = DialogResult.OK;
		}

		private void propertyGrid_PropertyValueChanged(object s, PropertyValueChangedEventArgs e)
		{
			if (e == null)
			{
				return;
			}
			GridItem changedItem = e.ChangedItem;
			if (changedItem == null)
			{
				return;
			}
			string label = changedItem.Label;
			List<string> list = new List<string>();
			list.Add("功率(W)");
			list.Add("功率系数");
			list.Add("额定电流(A)");
			if (this.layoutWay == EquipmentLayoutWay.Lay_SwitchBoxes)
			{
				foreach (string b in new List<string>
				{
					"宽度(mm)",
					"高度(mm)",
					"厚度(mm)"
				})
				{
					if (!(label != b))
					{
						string value = changedItem.Value.ToString();
						if (string.IsNullOrEmpty(value))
						{
							this.SetPropertyValue(label, e.OldValue);
							return;
						}
						if (Convert.ToDouble(value) < 100.0)
						{
							this.SetPropertyValue(label, e.OldValue);
							return;
						}
					}
				}
			}
			foreach (string b2 in list)
			{
				if (!(label != b2) && !Assist.StringUtil.isNumberic2(changedItem.Value.ToString()))
				{
					this.SetPropertyValue(label, e.OldValue);
					return;
				}
			}
			if (label == "功率系数")
			{
				double num = Convert.ToDouble(changedItem.Value);
				if (num < 0.0 || num > 1.0)
				{
					this.SetPropertyValue(label, e.OldValue);
				}
			}
		}

		private void SetPropertyValue(string name, object value)
		{
			YArchitech.Electric.Controls.HYPropertys hypropertys = this.propertyGrid.SelectedObject as YArchitech.Electric.Controls.HYPropertys;
			if (hypropertys == null)
			{
				return;
			}
			foreach (YArchitech.Electric.Controls.HYProperty hyproperty in hypropertys)
			{
				if (hyproperty.DisplayName == name)
				{
					hyproperty.Value = value;
					break;
				}
			}
		}

		private int LVM_SETICONSPACING = 4149;

		private const string XmlNode_LayoutWay = "LayoutWay";

		private const string XmlNode_Legend = "Legend";

		private const string XmlNode_Family = "Family";

		private const int GridColWidth = 100;

		private string key;

		private string formTitle;

		private int layoutWays;

		private List<DbFamilyGroup> familyGroups;

		private EquipmentLayoutWay layoutWay;

		private DbFamilyObject familyObject;
	}
}
